//
//  ScorePadAppDelegate.m
//  ScorePad
//
//  Created by Andy on 20/06/2010.
//  Copyright Mister Smith's Maevellous Mechanical Software Company 2010. All rights reserved.
//

#import "ScorePadAppDelegate.h"
#import "RootViewController.h"

@interface ScorePadAppDelegate ()

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *scoresArray;
@property (nonatomic, retain) NSArray *playerNames;
@property (nonatomic, retain) NSNumber *numberOfPlayers;

@end


@implementation ScorePadAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize scoresArray;
@synthesize playerNames;
@synthesize numberOfPlayers;


#pragma mark -
#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {

	// Alloc/Initialise the array of scores
	
	NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsPath = [path objectAtIndex:0];
	NSString *scoresPath = [documentsPath stringByAppendingPathComponent:@"scoresArray.plist"];
	
	NSMutableArray *scoresArrayFromPlist =[[NSMutableArray alloc] initWithContentsOfFile:scoresPath];
	
	if (scoresArrayFromPlist == nil) {	
		NSMutableArray *array1 = [[NSMutableArray alloc] init];
		
		self.scoresArray = array1;
		[array1 release];
		
		// Add a new hand to the array of scores		
		[self.scoresArray addObject:[NSArray arrayWithObjects:
									 [NSNumber numberWithInt:0],
									 [NSNumber numberWithInt:0],
									 [NSNumber numberWithInt:0],
									 [NSNumber numberWithInt:0],
									 [NSNumber numberWithInt:0],
									 [NSNumber numberWithInt:0],
									 [NSNumber numberWithInt:0],
									 nil]];
	}
	else {
		self.scoresArray = scoresArrayFromPlist;
		[scoresArrayFromPlist release];
	}
	
		
	// Initialise the other variables
	NSString *namesPath = [documentsPath stringByAppendingPathComponent:@"namesArray.plist"];
	
	NSMutableArray *namesArrayFromPlist = [[NSMutableArray alloc] initWithContentsOfFile:namesPath];
	
	if (namesArrayFromPlist == nil) {	
		
		NSArray *array2 = [[NSArray alloc]initWithObjects:
						   @"Player 1",
						   @"Player 2",
						   @"Player 3",
						   @"Player 4",
						   @"Player 5",
						   @"Player 6",
						   @"Player 7",
						   nil];
		self.playerNames = array2;
		[array2 release];
	}
	else {
		self.playerNames = namesArrayFromPlist;
	}
	
	[namesArrayFromPlist release];
	
	NSString *numberPath = [documentsPath stringByAppendingPathComponent:@"playersNumber.plist"];
	
	NSArray *playersNumberFromPlist =[[NSArray alloc] initWithContentsOfFile:numberPath];
	
	if (playersNumberFromPlist == nil) {
		NSNumber *number = [[NSNumber alloc]initWithInt:4];	
		self.numberOfPlayers = number;
		[number release];
	}
	else {
		self.numberOfPlayers = [playersNumberFromPlist objectAtIndex:0];
	}
	
	[playersNumberFromPlist release];
	
	// Alloc/Initialise the RootViewController and pass it the data
	RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];

	rootViewController.hands = self.scoresArray;
	rootViewController.playerNames = self.playerNames;
	rootViewController.numberOfPlayers = self.numberOfPlayers;

	// Alloc/Initialise the Navigation Controller with RootViewController
	UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
	self.navigationController = aNavigationController;
	[aNavigationController release];
	
	[rootViewController release];
	
	// Add a navigationController as 'window's first subview
	[window addSubview:[navigationController view]];
	
	// Make 'window' the key window and make that window visible
	[window makeKeyAndVisible];
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[navigationController release];
	[window release];
	[self.scoresArray release];
	[self.playerNames release];
	[self.numberOfPlayers release];
	
	[super dealloc];
}


@end

